﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ECare.BUS.Data;
using ECare.Database;

namespace ECare.BUS
{
    public class ResourcesHelper
    {


        public static List<ResoucesInfo> GetAllResources()
        {
            using (var context = new Entities())
            {
                var res = new List<ResoucesInfo>();

                var resources = context.Resources.ToList();
                foreach (var resource in resources)
                {
                    var newresource = new ResoucesInfo();
                    newresource.ResouceImage = resource.resource_image;
                    newresource.ResouceValidate = resource.resource_validate.HasValue
                        ? resource.resource_validate.Value
                        : 30;
                    newresource.ResourceCode = resource.resource_code;
                    newresource.ResourceId = resource.resource_Id;
                    newresource.ResourceName = resource.resource_name;
                    newresource.ResourceRegion = resource.resource_region;
                    newresource.ResourceSupply = resource.resource_supply;
                    newresource.ResourceTs = resource.resource_TS;
                    newresource.ResourceUnit = resource.resource_unit;

                    res.Add(newresource);
                }
                return res;
            }
        }



        public static ResoucesInfo GetResourcesByResourceId(int resourceId)
        {
            using (var context = new Entities())
            {
 
                var resource = context.Resources.FirstOrDefault(a => a.resource_Id == resourceId);


                if (resource == null)
                    return null;


                    var newresource = new ResoucesInfo();
                    newresource.ResouceImage = resource.resource_image;
                    newresource.ResouceValidate = resource.resource_validate.HasValue
                        ? resource.resource_validate.Value
                        : 30;
                    newresource.ResourceCode = resource.resource_code;
                    newresource.ResourceId = resource.resource_Id;
                    newresource.ResourceName = resource.resource_name;
                    newresource.ResourceRegion = resource.resource_region;
                    newresource.ResourceSupply = resource.resource_supply;
                    newresource.ResourceTs = resource.resource_TS;
                    newresource.ResourceUnit = resource.resource_unit;

                return newresource;

            }
        }



        public static int CreateResource(ResoucesInfo info)
        {
            try
            {
                using (var context = new Entities())
                {
                    var newresource = new Resource();
                    newresource.resource_image = info.ResouceImage;
                    newresource.resource_code = info.ResourceCode;
                    newresource.resource_name = info.ResourceName;
                    newresource.resource_region = info.ResourceRegion;
                    newresource.resource_supply = info.ResourceSupply;
                    newresource.resource_TS = info.ResourceTs;
                    newresource.resource_unit = info.ResourceUnit;
                    newresource.resource_validate = info.ResouceValidate;
                    context.Resources.AddObject(newresource);
                    context.SaveChanges();
                    return newresource.resource_Id;
                }
            }
            catch
            {
                return -1;
            }
        }


        public static byte[] GetResourceImage(int resourceId)
        {
            using (var context = new Entities())
            {
                var findres = context.Resources.FirstOrDefault(a => a.resource_Id == resourceId);
                if (findres != null)
                    return findres.resource_image;

                else
                    return null;
            }
        }

        public static bool UpdateResource(ResoucesInfo editinfo)
        {
            try
            {
                using (var context = new Entities())
                {
                    var findres = context.Resources.FirstOrDefault(a => a.resource_Id == editinfo.ResourceId);
                    if (findres != null)
                    {
                        findres.resource_image = editinfo.ResouceImage;
                        findres.resource_code = editinfo.ResourceCode;
                        findres.resource_name = editinfo.ResourceName;
                        findres.resource_region = editinfo.ResourceRegion;
                        findres.resource_supply = editinfo.ResourceSupply;
                        findres.resource_TS = editinfo.ResourceTs;
                        findres.resource_unit = editinfo.ResourceUnit;
                        findres.resource_validate = editinfo.ResouceValidate;
                    }


                    //remove the current details.



                    context.SaveChanges();
                    return true;
                }
            }
            catch (Exception ex)
            {
                return false;
            }




        }

        public static string GetHalfBarcodeInProducing(int resourceId)
        {
            using (var context = new Entities())
            {
                var barcode = context.sp_getrequiredInProducing(resourceId).FirstOrDefault().ToString();



                if (string.IsNullOrEmpty(barcode))
                    return string.Empty;
                else
                    return barcode.ToString();

            }
        }


        public static decimal GetResourceTotalInProducing(int resourceId)
        {
            using (var context = new Entities())
            {
                var total = context.sp_getResourceTotalInProducing(resourceId).FirstOrDefault();
                return total.HasValue ? total.Value : 0;
            }
        }

        public static bool UpdatePlanEndTime(int detail_id)
        {
            using (var context = new Entities())
            {
                var finddetail = context.plan_detail.FirstOrDefault(a => a.plan_detail_id == detail_id);

                if (finddetail == null)
                    return false;
                finddetail.plan_detail_end_time = DateTime.Now;
                finddetail.plan_detail_status = (int)PlanStatus.done;
                context.SaveChanges();
                return true;
            }
        }

        public static bool UpdatePlanStartTime(int detail_id)
        {
            using (var context = new Entities())
            {
                var finddetail = context.plan_detail.FirstOrDefault(a => a.plan_detail_id == detail_id);

                if (finddetail == null)
                    return false;

                if (!finddetail.plan_detail_start_time.HasValue)
                {
                    finddetail.plan_detail_start_time = DateTime.Now;
                    //   finddetail.plan_detail_status = (int)PlanStatus.started;
                }
                context.SaveChanges();
                return true;
            }
        }


        public static List<StorageInfo> SearchStorage()
        {
            using (var context = new Entities())
            {
                var results = new List<StorageInfo>();
                var res = context.sp_querystorage().ToList();

                foreach (var storage in res)
                {
                    var newquery = new StorageInfo();
                    newquery.resource_id = storage.resource_Id.Value;
                    newquery.supply_code = storage.supply_code;
                    newquery.Leftcount = storage.leftcount.Value;
                    newquery.unit_kg = storage.supply_unit_kg.Value;

                    results.Add(newquery);
                }
                return results;
            }
        }


        public static string GetResourceName(int resourceId)
        {
            using (var context = new Entities())
            {
                var findresult = context.Resources.FirstOrDefault(a => a.resource_Id == resourceId);
                if (findresult == null)
                    return "未知物料";

                else
                {
                    return string.Format("{0}({1})", findresult.resource_name, findresult.resource_code);
                }
            }
        }

        public static string GetResourceCode(int resourceId)
        {
            using(var context=new Entities())
            {
                                var findresult = context.Resources.FirstOrDefault(a => a.resource_Id == resourceId);
                if (findresult == null)
                    return "";
                else 
                    return findresult.resource_code;
            }
        }


       

  


        public static List<ProducingStorageInfo> SearchProducingStorage()
        {
            using (var context = new Entities())
            {
                var result = new List<ProducingStorageInfo>();

                var res = context.sp_sp_queryproducingstorage().ToList();
                foreach (var storage in res)
                {
                    var newquery = new ProducingStorageInfo();
                    newquery.resource_id = storage.resource_Id.Value;
                    newquery.total = storage.total.Value;
                    result.Add(newquery);

                }

                return result;
            }
        }


    }
}
